package com.lancer.platform.pay.utils;

import java.util.ArrayList;
import java.util.List;

public class ListUtils {

    public static <T> List<T> filter(List<T> list, ListUtilsHook<T> hook) {
        ArrayList<T> r = new ArrayList<T>();
        for (T t : list) {
            if (hook.test(t)) {
                r.add(t);
            }
        }
        r.trimToSize();
        return r;
    }

    public static <T> List<T> filter(List<T> list, ListUtilsHookUp<T> hook) {
        ArrayList<T> r = new ArrayList<T>();
        for (T t : list) {
            if (hook.test(t)) {
                r.add(hook.doing(t));
            }
        }
        r.trimToSize();
        return r;
    }



    public static <T> List<String> filterAndToString(List<T> list, ListUtilsHook<T> hook) {
        ArrayList<String> r = new ArrayList<>();
        for (T t : list) {
            if (hook.test(t)) {
                r.add(t.toString());
            }
        }
        r.trimToSize();
        return r;
    }

    public static String joining(List<String> list,CharSequence delimiter){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(delimiter);
        }
        return list.isEmpty()?"":sb.toString().substring(0, sb.toString().length() - 1);
    }

    public static String joining(List<String> list,CharSequence delimiter,CharSequence prefix, CharSequence suffix){
        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        sb.append(joining(list,delimiter));
        sb.append(suffix);
        return sb.toString();
    }
}
